﻿<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page import="com.cxx.purchasecharge.component.utils.ComponentConstants" %>
<%@ page import="com.cxx.purchasecharge.core.config.PurchaseChargeProperties"%>
	
	<style type="text/css">
		#dlg-user input, #dlg-user select
		{
			width: 150px;
		}
		#dlg-user #fm-user div 
		{
			float: left;
			width: 155px;
			margin: 5px 30px;
		}
	</style>
	
	<script type="text/javascript">
		$(function(){
            $('#searchbox-user').textbox().textbox('addClearBtn', 'icon-clear');
        });
		var hideSelectRolePanelAfterSelect = function() 
		{
			var roles = $('#dlg-user #fm-user #role').combotree('getValues');
			//console.log('roles --- ' + roles);
			$('#dlg-user #fm-user #roles').val(roles);
		}
		var cellFormatter_userRole = function(value,row,index)
		{
			var roles = '';
			if(row.roleBeans)
			{
				if(row.roleBeans.length >= 1) 
				{
					for(var i = 0; i < row.roleBeans.length; i++) 
					{
						roles += (row.roleBeans[i].name + ',');
					}
					roles = roles.substring(0, roles.length - 1);
				}
			}
			return '<span title="' + roles + '">' + roles + '</span>';
		}
		var newUserCallback = function() 
		{
			$('#dlg-user #fm-user #userId').removeAttr('readonly');
		}
		var editUserCallback = function() 
		{
			var user = $('#dg-user').datagrid('getSelected');
			//console.log(user);
			if(user) 
			{
				$('#dlg-user #fm-user #userId').attr('readonly', 'readonly');
				var roles = user.roleBeans;
				if(roles && roles.length > 0) 
				{
					var roleIds = new Array(roles.length);
					for(var i = 0; i < roles.length; i ++) 
					{
						roleIds[i] = roles[i].id;
					}
					$('#dlg-user #fm-user #role').combotree('setValues', roleIds);
					hideSelectRolePanelAfterSelect();
					
				}
				
				// handle checkbox issue
				if(user.enabled) 
				{
					$('#dlg-user #fm-user #enabled')[0].checked = true;
				}
				else 
				{
					$('#dlg-user #fm-user #enabled')[0].checked = false;
				}
				if(user.system) 
				{
					$('#dlg-user #fm-user #system')[0].checked = true;
				}
				else 
				{
					$('#dlg-user #fm-user #system')[0].checked = false;
				}
			}
		}
		var onClickUserRow = function (rowIndex, rowData) 
		{
			$('#toolbar-user #btn-editUser').linkbutton('enable');
			$('#toolbar-user #btn-delUser').linkbutton('enable');
			$('#toolbar-user #btn-resetPwd').linkbutton('enable');
		}
		var onLoadUserSuccess = function (data) 
		{
			$('#toolbar-user #btn-editUser').linkbutton('disable');
			$('#toolbar-user #btn-delUser').linkbutton('disable');
			$('#toolbar-user #btn-resetPwd').linkbutton('disable');
		}
		
		var resetUserPwd = function () 
		{
			var row = $('#dg-user').datagrid('getSelected');
			if(row) 
			{
				var userId = row.userId;
				var password = '<%=PurchaseChargeProperties.getDefaultPassword()%>';
				$.messager.confirm('确认',
						'您确认要重置' + userId + '的密码为' + password + '?', function(r) {
							if (r) {
								ajaxPostRequest('<c:url value='/password/resetPassword.html' />', {userId:row.userId});
							}
				});
			}
		}
		var cellFormatter_gender = function(value,row,index) 
		{
			if(value == "MALE") 
			{
				return "男";
			}
			if(value == "FEMALE") 
			{
				return "女";
			}
			return "未知";
		}
    </script>

	<table id="dg-user" title="<spring:message code="userManagement" />" class="easyui-datagrid" url="<c:url value='/user/getUsers.html' />"
		toolbar="#toolbar-user" pagination="true" rownumbers="true" fit="true"
		 singleSelect="true" checkOnSelect="true" selectOnCheck="false"
		fitColumns="true" sortName="userId" sortOrder="asc" data-options="onClickRow:onClickUserRow, onLoadSuccess:onLoadUserSuccess">
		<thead>
			<tr>
				<th data-options="field:'ck',checkbox:true"></th>
				<th data-options="field:'id',hidden:true"></th>
				<th field="userId" width="50" sortable="true"><spring:message code="userId" /></th>
				<th field="firstName" width="30"><spring:message code="firstName" /></th>
				<th field="lastName" width="30"><spring:message code="lastName" /></th>
				<th field="genderCode" width="30" sortable="true" data-options="formatter:cellFormatter_gender">性别</th>
				<th field="bod" width="30" sortable="true">生日</th>
				<!-- <th field="pwd" width="50">密码</th> -->
				<th field="enabled" width="30" sortable="true" data-options="formatter:cellFormatter_enable, align:'center'">可用</th>
				<th field="system" width="30" sortable="true" data-options="formatter:cellFormatter_enable, align:'center'">系统账户</th>
				<th field="<%=ComponentConstants.PHONE %>" width="50"><spring:message code="phone" /></th>
				<th field="email" width="50" sortable="false"><spring:message code="email" /></th>
				<th field="role" width="50" sortable="false" data-options="formatter:cellFormatter_userRole, styler:cellStyler_userRole">角色</th>
			</tr>
		</thead>
	</table>
	
	<div id="toolbar-user">
		<a href="javascript:void(0)" class="easyui-linkbutton" 
			iconCls="icon-add" plain="true" onclick="newModel('#dlg-user', '<spring:message code="newUser" />', '#fm-user', '<c:url value='/user/addModel.html' />', newUserCallback)" title="<spring:message code="newUser" />">新增用户</a> 
		<a id="btn-editUser" href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editModel('#dg-user', '#dlg-user', '<spring:message code="editUser" />', '#fm-user', '<c:url value='/user/updateModel.html' />', editUserCallback)" title="<spring:message code="selectOneRow" /><spring:message code="editUser" />">编辑用户</a> 
		<a id="btn-delUser" href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyMultipleModel('#dg-user', '<spring:message code="user" />', '<c:url value='/user/deleteModels.html' />')" title="<spring:message code="checkOneOrMultiple" /><spring:message code="removeUser" />">删除用户</a>
		<a id="btn-resetPwd" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-back" plain="true" onclick="resetUserPwd()" title="<spring:message code="selectOneRow" />重置密码，默认为<%=PurchaseChargeProperties.getDefaultPassword()%>">重置密码</a>
		<span style="float: right; margin-right: 5px">
			<input id="searchbox-user" class="easyui-textbox" data-options="width:200, prompt:'<spring:message code="pleaseInputValue" />', icons:[{
                iconCls:'icon-search',
                handler: function(e){
                    var v = $(e.data.target).textbox('getValue');
                    doSearchUser(v);
                }
            }]" />
		</span>
	</div>

	<div id="dlg-user" class="easyui-dialog"
		style="width: 800px; height: 320px; padding: 20px 5px" closed="true"
		buttons="#dlg-buttons-user" data-options="modal:true">
		<div style="width:100%;height:100%">
			<form id="fm-user" class="fm" style="margin:0 auto;padding:0px;width:650px;height:100%;" method="post" novalidate>
				<div class="fitem divHidden">
					<input id="userUniqueId" name="id" value="0">
				</div>
				<div class="fitem">
					<label><spring:message code="userId" />:</label> 
					<input id="userId" name="userId" class="easyui-textbox" required="true"
					validType="myRemote['<c:url value='/user/checkExist.html' />', 'userId', '#userUniqueId']">
				</div>
				<div class="fitem">
					<label><spring:message code="firstName" />:</label> 
					<input name="firstName" class="easyui-textbox" required="true">
				</div>
				<div class="fitem">
					<label><spring:message code="lastName" />:</label> 
					<input name="lastName" class="easyui-textbox" required="true">
				</div>
				<div class="fitem">
					<label>性别:</label> 
					<select name="genderCode" class="easyui-combobox" style="width:150px" editable="false" panelHeight="auto" data-options="">
						<option value="MALE" selected="selected"><spring:message code="contact.option.male" /></option>
						<option value="FEMALE"><spring:message code="contact.option.female" /></option>
					</select>
				</div>
				<div class="fitem">
					<label>生日:</label> 
					<input name="bod" class="easyui-datebox" editable="false" style="width:150px">
				</div>
				<div class="fitem">
					<label><spring:message code="phone" />:</label> 
					<input name="<%=ComponentConstants.PHONE %>" class="easyui-textbox" validType="phoneCheck" />
				</div>
				<div class="fitem">
					<label><spring:message code="email" />:</label> 
					<input name="email" class="easyui-textbox" validType="email">
				</div>
				<div class="fitem">
					<label>可用:</label> 
					<input id="enabled" name="enabled" type="checkbox" style="width:30px; height:18px" />
				</div>
				<div class="fitem">
					<label>是否系统:</label> 
					<input id="system" name="system" type="checkbox" style="width:30px; height:18px" />
				</div>
				<div class="fitem">
					<label>角色:</label> 
					<input name="role" id="role" class="easyui-combotree" panelHeight="auto" style="width:150px" 
						data-options="url:'<c:url value='/role/getAllModel.html' />',required:true, 
						multiple:true, checkbox:true, 
						onHidePanel:hideSelectRolePanelAfterSelect">
					<input name="roles" id="roles" type="hidden" />
				</div>
			</form>
		</div>
	</div>
	<div id="dlg-buttons-user">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-ok" onclick="saveModel('#dg-user', '#dlg-user', '#fm-user')"><spring:message code="save" /></a> 
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-cancel" onclick="javascript:$('#dlg-user').dialog('close')"><spring:message code="cancel" /></a>
	</div>